<?php
namespace App\Services\Api;
use Illuminate\Contracts\Cache;

class SendMessage
{
    const REMEMBER_MINS = 5;
    const ITEMS_HOUR = 24;
    /**
     * @param $phone  手机号码
     *
     */
    public function getMessage($phone){

        if(!$phone){
            return ['code'=>1,'msg'=>'电话号码为空'];
        }
        if(preg_match("/^1[34578]\d{9}$/", $phone)){

                $sms_account = config('securitysetting.sms_account');
                $sms_password = config('securitysetting.sms_password');
                $sendcode=mt_rand(1000,9999);
                $msg="验证码:".$sendcode.",有效时间:".self::REMEMBER_MINS."分钟,请尽快完成验证";
                $fsurl="http://send.18sms.com/msg/HttpBatchSendSM?account=".$sms_account."&pswd=".$sms_password."&mobile=" . $phone . "&msg=" . $msg . "&needstatus=true";
                $res=file_get_contents($fsurl);
                $res = explode('\n',$res);
                $res = explode(',',$res[0]);
                if(0 == $res[1]){
                    \Cache::put($phone,$sendcode,self::REMEMBER_MINS);
                    return ['code'=>0,'msg'=>'验证码已发送，请注意查收'];
                }else{
                    return ['code'=>1,'msg'=>'短信发送失败'];
                }


        }else {
            return ['code'=>1,'msg'=>'手机号码格式不正确'];
        }
    }

    /**
     * 配单发送短信通知
     * @param $phone  手机号码
     *
     */
    public function getItemsMessage($phone){

        if(!$phone){
            return ['code'=>1,'msg'=>'电话号码为空'];
        }
        if(preg_match("/^1[34578]\d{9}$/", $phone)){
                $sms_account = config('securitysetting.sms_account');
                $sms_password = config('securitysetting.sms_password');
                $msg="您有新的配单信息,有效时间:".self::ITEMS_HOUR."小时,请尽快下单";
                $fsurl="http://send.18sms.com/msg/HttpBatchSendSM?account=".$sms_account."&pswd=".$sms_password."&mobile=" . $phone . "&msg=" . $msg . "&needstatus=true";
                $res=file_get_contents($fsurl);
                $res = explode('\n',$res);
                $res = explode(',',$res[0]);
                if(0 == $res[1]){
                    return ['code'=>0,'msg'=>'短信通知已发送'];
                }else{
                    return ['code'=>1,'msg'=>'短信发送失败'];
                }
        }else {
            return ['code'=>1,'msg'=>'手机号码格式不正确'];
        }
    }


}